package com.flute.icrawler.framework.framework;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 一个processor 的集合
 * 
 * @author Administrator
 * 
 */
public class ProcessorList {
	private HashMap<String, IProcessor> processorList;

	public ProcessorList() {
		this.processorList = new HashMap<String, IProcessor>();
	}

	public synchronized void addProcessor(IProcessor processor) {
		this.processorList.put(processor.getClass().getName(), processor);
	}

	public synchronized List<IProcessor> getProcessorList() {
		return Arrays.asList(processorList.values()
				.toArray(new IProcessor[] {}));
	}

	public String[] getProcessorNames() {
		return processorList.keySet().toArray(new String[] {});
	}

	public void setProcessorList(HashMap<String, IProcessor> processorList) {
		this.processorList = processorList;
	}

	public boolean contains(IProcessor processor) {
		return processorList.containsKey(processor.getClass().getName());
	}

	public boolean contains(String name) {
		return processorList.containsKey(name);
	}

	public int size() {
		return processorList.size();
	}

	public boolean containsAll(ProcessorList list) {
		for (String name : list.getProcessorNames()) {
			if (!processorList.containsKey(name)) {
				return false;
			}
		}

		return true;
	}
	
	public boolean containsAllNames(ProcessorList list) {
		for (String name : list.getProcessorNames()) {
			if (!processorList.containsKey(name)) {
				return false;
			}
		}

		return true;
	}

	public void addAll(ProcessorList list) {
		for (IProcessor processor : list.getProcessorList()) {
			addProcessor(processor);
		}
	}

	public ProcessorList removeAll(ProcessorList list) {
		ProcessorList remain = new ProcessorList();
		for (String key : processorList.keySet()) {
			if (!list.contains(key)) {
				remain.addProcessor(processorList.get(key));
			}
		}

		return remain;
	}

	public ProcessorList retainAll(ProcessorList list) {
		ProcessorList remain = new ProcessorList();
		for (String key : processorList.keySet()) {
			if (list.contains(key)) {
				remain.addProcessor(processorList.get(key));
			}
		}

		return remain;
	}

	@Override
	public int hashCode() {
		StringBuffer sb = new StringBuffer();
		for (String key : processorList.keySet()) {
			sb.append(key + ":");
		}

		return sb.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final ProcessorList other = (ProcessorList) obj;
		if (processorList == null) {
			if (other.processorList != null)
				return false;
		} else if (hashCode() != other.hashCode())
			return false;
		return true;
	}

	public String toString() {
		return this.getClass().getSimpleName() + "[" + "processorList="
				+ this.processorList + "]";
	}

	public String toKey() {
		StringBuffer sb = new StringBuffer();
		for (IProcessor p : processorList.values()) {
			sb.append(p.getClass().getSimpleName());
		}

		return sb.toString();
	}

}
